﻿#include "includes\\InputStructures.fx"
#include "includes\\CommonResources.fx"
#include "includes\\StateDefinitions.fx"
#include "includes\\BlinnShading.fx"
#include "includes\\HelperFunctions.fx"

float xSuperSamplingFactor;
texture2D xReflectionTexture;
texture2D xPlatformTexture;
float3 xPlatformLightColor;
float2 xWorkingResolution;


struct PsInput_Platform
{
	float4 ScreenPos : SV_POSITION;
	float3 Pos3D : POSITION;
	float2 UV: TEXCOORD;
	float3 Normal : NORMAL;
};


PsInput_Platform PlatformVS(VsInput_PTN input)
{
	PsInput_Platform output = (PsInput_Platform)0;
	
	output.ScreenPos = mul(input.Pos, xViewProjectionMatrix);
	output.Pos3D = input.Pos.xyz;
	output.UV = input.UV;
	output.Normal = normalize(input.Normal.xyz);
	
	return output;
}


float GetLightIntensity1D(float relativePos, float exponent)
{
	return pow(abs(relativePos - 0.5f) * 2.1f, exponent);
}


float GetLightIntensity2D(float2 relativePos, float exponent)
{
	return GetLightIntensity1D(relativePos.x, exponent) + GetLightIntensity1D(relativePos.y, exponent);
}


float3 GetPlatformLightValue(float3 pos3D, float2 relativePos)
{
	float distFactor = 100.0f / sqrt(GetCameraDistance(pos3D));
	float platformLightIntensity = GetLightIntensity2D(relativePos, distFactor);
	return platformLightIntensity * xPlatformLightColor;
}


float3 GetReflection(float2 screenPos)
{
	float2 uv = float2(1.0f - screenPos.x / xWorkingResolution.x, screenPos.y / xWorkingResolution.y);
	return xReflectionTexture.Sample(PlanarSampler, uv) * 0.33f;
}


float4 PlatformPS(PsInput_Platform input) : SV_Target
{
	float3 sampledColor = xPlatformTexture.Sample(PlanarSampler, input.UV).rgb;

	float3 result = GetBlinnShadedColor(sampledColor, input.Pos3D, input.Normal, cGlobalLightPosition, 96.0f, 0.5f);
	result += GetPlatformLightValue(input.Pos3D, input.UV);
	result += GetReflection(input.ScreenPos.xy);

	return float4(result.rgb, 1.0f);
}


float4 PlatformPS_NoReflection(PsInput_Platform input) : SV_Target
{
	float3 sampledColor = xPlatformTexture.Sample(PlanarSampler, input.UV).rgb;

	float3 result = GetBlinnShadedColor(sampledColor, input.Pos3D, input.Normal, cGlobalLightPosition, 96.0f, 0.5f);
	result += GetPlatformLightValue(input.Pos3D, input.UV);

	return float4(result.rgb, 1.0f);
}